<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class MessageController extends Controller
{
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //发送消息
//        return view('/home/myhouse/message/add');
        return view('/home/myhouse/message/create');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
//        dd($request ->all());
        //如果是超级会员查询所有数据
        if (false) {
            $data = DB::table('message')->where('readed','1')->where(function($query) use ($request){
                $query -> where('content','like','%'.$request -> input('keywords').'%');
            }) -> paginate($request -> input('num',3));
        } else{
            $data = DB::table('message')->where('receive_mem_id',1)->where('readed','1')->where(function($query) use ($request){
                $query -> where('content','like','%'.$request -> input('keywords').'%');
            }) -> paginate($request -> input('num',3));
        }
        foreach($data as $k=>$v)
        {

            //取出发信人用户名
            $send_mem_id  = $v -> send_mem_id;
            //获取收信人名
            $receive_id =$v -> receive_mem_id;
            $arr = DB::table('member') -> where('member_id',$send_mem_id) -> first();
            $arr1 = DB::table('member') -> where('member_id',$receive_id) -> first();
            $v->sname = $arr->name;
            $v->rname = $arr1->name;
            //  $v->stime = date('Y-m-d H:i:s',(date()-$v->stime));
            $v->stime = date('Y-m-d H:i:s',$v->stime);
            $v->content = mb_substr(trim($v->content),0,20);
        }
        return view('/home/myhouse/message/index',['data'=>$data,'request' => $request -> all()]);
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //处理添加
        $data = $request -> all();
        $name=$data['name'];
        $content=$data['content'];
        $userarr=DB::table('member') -> select('name')->get();
        foreach($userarr as $v)
        {
            $names[] = $v->name;
        }
        if(!in_array($name,$names))
        {
            return redirect('/home/myhouse/message/create') -> with(['success'=>'用户名不存在',
                'content'=> $content]);
        };
        $arr = DB::table('member') -> where('name',$name)->first();
        unset($data['_token']);
        unset($data['name']);
        $data['receive_mem_id'] = $arr->member_id;
        $data['stime'] = time();
        $res=DB::table('message')->insert($data);
        if($res)
        {
            return redirect('/home/myhouse/message/create') -> with(['success'=>'发送成功']);
        }else{
            return redirect('/home/myhouse/message/create') -> with(['success'=>'发送失败']);
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //消息详情
        $data = DB::table('message')->where('receive_mem_id',$id) ->get();
        //根据ID获取发件人、收件人name
        $arr = DB::table('member') -> where('member_id',$data['0'] ->send_mem_id) -> first();
        $arr1 = DB::table('member') -> where('member_id',$data['0'] ->receive_mem_id) -> first();
        //更改为已读
        DB::table('message')->where('receive_mem_id',$id)->where('send_mem_id',$data['0'] ->send_mem_id) ->update(['readed'=>2]);
        $data['0'] ->sname = $arr->name;
        $data['0'] ->rname = $arr1->name;
        $data['0'] ->time = $data['0']->stime;
        $data['0']->stime = date('Y-m-d H:i:s',$data['0']->stime);
        $data=$data['0'];
        return view('/home/myhouse/message/show',['data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除消息
        $res=DB::table('message')->where('stime',$id)->update(['disabled'=>2]);
        if($res)
        {
            return redirect('/home/myhouse/message') -> with(['success'=>'已移入回收站']);
        }else{
            return redirect('/home/myhouse/message') -> with(['success'=>'删除失败']);
        }

    }

    //收件箱
    public function receive(Request $request)
    {
        $data = DB::table('message')->where('receive_mem_id',1)->where(function($query) use ($request){
            $query -> where('content','like','%'.$request -> input('keywords').'%');
        }) -> paginate($request -> input('num',3));
        foreach($data as $k=>$v)
        {

            //取出发信人用户名
            $send_mem_id  = $v -> send_mem_id;
            //获取收信人名
            $receive_id =$v -> receive_mem_id;
            $arr = DB::table('member') -> where('member_id',$send_mem_id) -> first();
            $arr1 = DB::table('member') -> where('member_id',$receive_id) -> first();
            $v->sname = $arr->name;
            $v->rname = $arr1->name;
            //  $v->stime = date('Y-m-d H:i:s',(date()-$v->stime));
            $v->stime = date('Y-m-d H:i:s',$v->stime);
            $v->content = mb_substr(trim($v->content),0,20);
        }
        return view('/home/myhouse/message/receive',['data'=>$data,'request' => $request -> all()]);
    }
    //已发送
    public function send(Request $request)
    {
        $data = DB::table('message')->where('send_mem_id',1)->where(function($query) use ($request){
            $query -> where('content','like','%'.$request -> input('keywords').'%');
        }) -> paginate($request -> input('num',3));
        foreach($data as $k=>$v)
        {
            //取出发信人用户名
            $send_mem_id  = $v -> send_mem_id;
            //获取收信人名
            $receive_id =$v -> receive_mem_id;
            $arr = DB::table('member') -> where('member_id',$send_mem_id) -> first();
            $arr1 = DB::table('member') -> where('member_id',$receive_id) -> first();
            $v->sname = $arr->name;
            $v->rname = $arr1->name;
            //  $v->stime = date('Y-m-d H:i:s',(date()-$v->stime));
            $v->stime = date('Y-m-d H:i:s',$v->stime);
            $v->content = mb_substr(trim($v->content),0,20);
        }
        return view('/home/myhouse/message/send',['data'=>$data,'request' => $request -> all()]);
    }

    //已删除
    public function delete(Request $request)
    {
        $data = DB::table('message')->where('receive_mem_id',1)
            ->where('disabled',2)
            ->where(function($query) use ($request){
                $query -> where('content','like','%'.$request -> input('keywords').'%');
            }) -> paginate($request -> input('num',3));
        foreach($data as $k=>$v)
        {
            //取出发信人用户名
            $send_mem_id  = $v -> send_mem_id;
            //获取收信人名
            $receive_id =$v -> receive_mem_id;
            $arr = DB::table('member') -> where('member_id',$send_mem_id) -> first();
            $arr1 = DB::table('member') -> where('member_id',$receive_id) -> first();
            $v->sname = $arr->name;
            $v->rname = $arr1->name;
            //  $v->stime = date('Y-m-d H:i:s',(date()-$v->stime));
            $v->time=$v->stime;
            $v->stime = date('Y-m-d H:i:s',$v->stime);
            $v->content = mb_substr(trim($v->content),0,20);
        }
        return view('/home/myhouse/message/delete',['data'=>$data,'request' => $request -> all()]);
    }

    //执行删除
    public function dodelete($id)
    {
        $res=DB::table('message')->where('stime',$id)->delete();
        if($res)
        {
            return redirect('/home/myhouse/message') -> with(['success'=>'删除成功']);
        }else{
            return redirect('/home/myhouse/message') -> with(['success'=>'删除失败']);
        }
    }
}
